
package com.avrio.ajax;

// <editor-fold defaultstate="collapsed" desc="import">
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.avrio.utils.MySQL;
// </editor-fold>

@WebServlet(name="GetData",loadOnStartup=1,urlPatterns={"/get"})
public class GetData extends HttpServlet {

    private final static Logger log = Logger.getLogger(GetData.class.getName());

    private MySQL mysql;

// <editor-fold defaultstate="collapsed" desc="init()">
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        mysql = new MySQL();
        if(mysql == null) {
            log.log(Level.SEVERE, "mysql=null");
        }
        log.log(Level.INFO, "Init");
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="processRequest()">

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {

        String doval = mysql.getString(request.getParameter("do"), "", 15);
        String exval = mysql.getString(request.getParameter("ex"), "", 10);
        int start    = mysql.getInt(request.getParameter("start"), 0);
        int limit    = mysql.getInt(request.getParameter("limit"), 15);
        String sort  = mysql.getString(request.getParameter("sort"), "email", 15);
        String dir   = mysql.getString(request.getParameter("dir"), "ASC", 15);

        String json;
        switch(doval) {
// <editor-fold defaultstate="collapsed" desc="Person">
            case "person":
                switch(exval) {
                    case "read":
                        json = mysql.loadPerson(start, limit, sort, dir);
                        break;
                    case "update":
                        json = mysql.updatePerson(request);
                        break;
                    case "create":
                        json = mysql.createPerson(request);
                        break;
                    case "delete":
                        json = mysql.deletePerson(request);
                        break;
                    default:
                        json = "{success:false}";
                }
                break;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="DeviceType">
            case "devicetype":
                switch(exval) {
                    case "read":
                        json = mysql.loadDeviceType(start, limit, sort, dir);
                        break;
                    case "update":
                        json = mysql.updateDeviceType(request);
                        break;
                    case "create":
                        json = mysql.createDeviceType(request);
                        break;
                    case "delete":
                        json = mysql.deleteDeviceType(request);
                        break;
                    default:
                        json = "{success:false}";
                }
                break;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="DeviceSession">
            case "devicesession":
                switch(exval) {
                    case "read":
                        json = mysql.loadDeviceSession(start, limit, sort, dir);
                        break;
                    case "update":
                        json = mysql.updateDeviceSession(request);
                        break;
                    case "create":
                        json = mysql.createDeviceSession(request);
                        break;
                    case "delete":
                        json = mysql.deleteDeviceSession(request);
                        break;
                    default:
                        json = "{success:false}";
                }
                break;
// </editor-fold>
            default:
                json = "{success:false}";
        }

        response.setContentType("text/html;charset=UTF-8");
        try(PrintWriter out = response.getWriter()) {
            out.print(json);
        }
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="methods">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "GetData";
    }

    @Override
    public void destroy() {
        log.log(Level.INFO, "Destroy");
    }
// </editor-fold>
}
